package com.jtpay.walletu3d;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.pay.sdk.aidl.AidlUtils;
import com.pay.sdk.interfaces.GetPayStatusListener;
import com.pay.sdk.utils.WalletBrowerUtils;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;

public class MainActivity extends UnityPlayerActivity {
    private Toast mToast;
    private AidlUtils aidlUtils;
    private WalletBrowerUtils mBrowerUtils;
    private boolean isSDKPaying;
    private String payStatus;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }
    private void initView() {
        GetPayStatusListener payStatusListener = new GetPayStatusListener() {
            @Override
            public void onPayStatus(String payStatus) {
                Log.d("junfutong", "callback result=" + payStatus);
                MainActivity.this.payStatus=payStatus;
                UnityPlayer.UnitySendMessage("Canvas", "payStatusU3d", payStatus);
            }
        };
        aidlUtils = new AidlUtils(this, payStatusListener);
        mBrowerUtils = new WalletBrowerUtils(this);
    }
    @Override
    protected void onResume() {
        super.onResume();
        if (isSDKPaying) {
            isSDKPaying = false;
            aidlUtils.getPayStatus();
        }
    }

    /**wap*/
    public void wapPay(final String url){
        Log.d("junfutong", "pullup wap url=" + url);
        mBrowerUtils.pullUpWallet(url);
    }
    /**DownloadAddress*/
    public void setDownloadAddress(final String downloadAddress){
        mBrowerUtils.setDownLaodAddress(downloadAddress);
    }
    /**sdk*/
    public void sdkPay(final String url){
        Log.d("junfutong", "pullup sdk url=" + url);

        /*The sdk call can add a payout callback notification*/
        isSDKPaying = true;
        aidlUtils.bindAidlService();// bind aidl callback service
        mBrowerUtils.pullUpWallet(url);
    }
    /**
     * payBrower
     **/
    public void payBrower(final String  url) {
        mBrowerUtils.choiceBrower(url);
    }
    public void showToast(final String text) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {

            public void run() {
                if (mToast == null) {
                    mToast = Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT);
                } else {
                    mToast.setText(text);
                }
                mToast.show();
            }
        });
    }
}
